package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwgiT;
import cn.e4j.modular.crm.entity.GwgjT;
import cn.e4j.modular.crm.entity.GwgkT;
import cn.e4j.modular.crm.service.Gwgi005Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.List;

@Controller
@RequestMapping("/crm/gwgi005")
public class Gwgi005Controller extends BaseController {

    @Autowired
    Gwgi005Service gwgi005Service;

    /**
     * 跳转至招标信息维护主界面
     *
     * @return
     */
    @RequiresPermissions("crm:gwgi005:init")
    @RequestMapping(value = {"", "/init"})
    public String index() {
        return "/pc/crm/gwgi005.html";
    }


    @RequiresPermissions("crm:gwgi005:init")
    @RequestMapping("/getHeadList")
    @ResponseBody
    public ResponseData getHeadList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        List<Dto> list = gwgi005Service.getHeadList(page,inDto);
        return new PageReData(page, list);
    }

    @RequiresPermissions("crm:gwgi005:add")
    @RequestMapping("/getDetailData")
    @ResponseBody
    public ResponseData getDetailData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        List<Dto> gwgjTList = gwgi005Service.getGwgjData(inDto);
        List<GwgkT> gwgkTList = gwgi005Service.getGwgkData(inDto);
        outDto.put("gwgjList",gwgjTList);
        outDto.put("gwgkList",gwgkTList);
        return new SuccessResponseData(outDto);
    }

    @RequiresPermissions("crm:gwgi005:add")
    @RequestMapping("/getAddData")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        outDto.put("CURRENT_USER_ID", shiroUser.getID());
        outDto.put("CURRENT_USER_NAME", shiroUser.getNAME());
        outDto.put("CURRENT_ORG", shiroUser.getORG_ID());
        outDto.put("CURRENT_ORG_NAME", shiroUser.getSysOrgT().getNAME());
        outDto.put("CURRENT_DATE", LocalDate.now());
        return new SuccessResponseData(outDto);
    }


    @RequiresPermissions("crm:gwgi005:add")
    @RequestMapping("/add")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        String ENT = inDto.getString("ENT");

        GwgiT gwgiT = JSONObject.parseObject(jsonObject.getString("gwgiT"), GwgiT.class);
        gwgiT.setGWGIENT(ENT);

        if (gwgi005Service.existedTheParams(gwgiT)) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        }

        List<GwgjT> gwgjList = JSON.parseArray(jsonObject.getString("gwgjList"), GwgjT.class);
        gwgjList.forEach(gwgjT -> {
            gwgjT.setGWGJENT(ENT);
            gwgjT.setGWGJ001(gwgiT.getGWGI001());
            gwgjT.setGWGJID(gwgiT.getGWGIID());
        });

        List<GwgkT> gwgkList = JSON.parseArray(jsonObject.getString("gwgkList"), GwgkT.class);
        gwgkList.forEach(gwgkT -> {
            gwgkT.setGWGKENT(ENT);
            gwgkT.setGWGK001(gwgiT.getGWGI001());
            gwgkT.setGWGKID(gwgiT.getGWGIID());
        });
        gwgi005Service.add(gwgiT, gwgjList, gwgkList);

        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwgi005:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        String ENT = inDto.getString("ENT");

        GwgiT gwgiT = JSONObject.parseObject(jsonObject.getString("gwgiT"), GwgiT.class);
        gwgiT.setGWGIENT(ENT);

        List<GwgjT> gwgjList = JSON.parseArray(jsonObject.getString("gwgjList"), GwgjT.class);
        gwgjList.forEach(gwgjT -> {
            gwgjT.setGWGJENT(ENT);
            gwgjT.setGWGJ001(gwgiT.getGWGI001());
            gwgjT.setGWGJID(gwgiT.getGWGIID());
        });

        List<GwgkT> gwgkList = JSON.parseArray(jsonObject.getString("gwgkList"), GwgkT.class);
        gwgkList.forEach(gwgkT -> {
            gwgkT.setGWGKENT(ENT);
            gwgkT.setGWGK001(gwgiT.getGWGI001());
            gwgkT.setGWGKID(gwgiT.getGWGIID());
        });
        gwgi005Service.edit(gwgiT, gwgjList, gwgkList);

        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwgi005:del")
    @RequestMapping("/del")
    @ResponseBody
    public ResponseData del(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        gwgi005Service.delete(inDto);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwgi005:del")
    @RequestMapping("/delBatch")
    @ResponseBody
    public ResponseData delBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwgiT> gwgiTList = inDto.getReqList(GwgiT.class);
        gwgi005Service.deleteBatch(gwgiTList);
        return SUCCESS_TIP;
    }

    // 根据招标编号抓取投标明细
    @RequiresPermissions("crm:gwgi005:add")
    @RequestMapping("/getGwgdData")
    @ResponseBody
    public ResponseData getGwgdData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> resultList = gwgi005Service.getGwgdData(inDto);
        return new SuccessResponseData(resultList);
    }



    @RequiresPermissions("crm:gwgi005:edit")
    @RequestMapping("/changeStatus")
    @ResponseBody
    public ResponseData changeStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwgiT> gwgiTList = inDto.getReqList(GwgiT.class);
        gwgi005Service.changeStatusBatch(gwgiTList);
        return SUCCESS_TIP;
    }

    @RequestMapping("/getBookmarkData")
    @ResponseBody
    public ResponseData getBookmarkData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page page = dtoWapper.getPage();
        List<Dto> gwgiTList = gwgi005Service.getBookmarkData(page,inDto);
        return new PageReData(page, gwgiTList);
    }
}
